package order

import (
	"enjoy_wangyashuo/internal/model/mysql"
	"gorm.io/gorm"
)

type Order struct {
	gorm.Model
	UserId          int64   `gorm:"type:int(11);not null;comment:用户id"`
	OrderId         int64   `gorm:"type:int(11);not null;comment:订单id"`
	OrderNumber     string  `gorm:"type:varchar(50);uniqueIndex;not null;comment:订单编号"`
	ShippingAddress string  `gorm:"type:varchar(255);comment:收货地址"`
	OrderStatus     int64   `gorm:"type:int(11);not null;default:0;comment:订单状态,0:未处理,1:已处理;"`
	PaymentStatus   int64   `gorm:"type:int(11);not null;default:0;comment:支付状态: 0:未支付,1:已支付"` // 支付状态，默认是未支付
	TotalAmount     float64 `gorm:"type:decimal(10,2);not null;comment:支付金额"`
	Discount        float64 `gorm:"type:decimal(10,2);default:0.00;comment:优惠金额"` //优惠金额默认为0
	PaymentType     int64   `gorm:"type:int(11);not null;default:0;comment:支付方式，0:未选择，1:支付宝，2:微信，3:银行卡"`
}

// 创建订单
func (o *Order) CreateOrder() error {
	return mysql.WithMysql(func(db *gorm.DB) error {
		return db.Create(o).Error
	})
}

// 订单列表
func (o *Order) OrderList() (info []Order, err error) {
	var ordes []Order
	err = mysql.WithMysql(func(db *gorm.DB) error {
		return db.Where("order_id", o.OrderId).Find(&ordes).Error
	})
	return ordes, err
}
